package echat.future.cn.echat.my.api;

import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import echat.future.cn.echat.AppApplication;
import echat.future.cn.echat.my.form.AuthImg;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class PutObjectSamplesApi {
    public static Observable<String> upLoadFile(String testObject, String uploadFilePath) {
        return Observable.create((ObservableOnSubscribe<String>) emitter -> {
            if (uploadFilePath != null) {
                PutObjectRequest put = new PutObjectRequest("echat-voide", testObject, uploadFilePath);
                PutObjectResult putResult = AppApplication.oss.putObject(put);
                emitter.onNext("http://echat-voide.oss-cn-beijing.aliyuncs.com/" + testObject);
            } else {
                emitter.onNext("");
            }
            emitter.onComplete();
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<List<AuthImg>> upLoadFils(List<String> files) {
        return Observable.create((ObservableOnSubscribe<List<AuthImg>>) emitter -> {
            List<AuthImg> authImgs = new ArrayList<>();
            for (String file : files) {
                UUID uuid = UUID.randomUUID();
                PutObjectRequest put = new PutObjectRequest("echat-voide", uuid.toString().replace("-", ""), file);
                PutObjectResult putResult = AppApplication.oss.putObject(put);
                authImgs.add(new AuthImg("http://echat-voide.oss-cn-beijing.aliyuncs.com/" + uuid.toString().replace("-", "")));
            }
            emitter.onNext(authImgs);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io());
    }
}
